package cn.dida.controller;

import cn.dida.model.UserSession;
import cn.dida.service.QrcodeService;
import cn.dida.util.QRCodeUtil;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自动生成二维啊图片
 * Created by zhangzhiling on 2017/3/28.
 */
@Controller
public class AqrcodePhoto extends BaseController{

    @Autowired
    private QrcodeService qrcodeService;

    String qrcode_valid_url = "https://betascan.vats.com.cn/scan/";

    /**
     * 普通码
     * @param request
     * @param id
     * @param qcname
     * @param isAutoDraw
     * @return
     */
    @GetMapping("/download/qrcodeImage")
    public ModelAndView getQrList(HttpServletRequest request, Integer id, String qcname,Integer isAutoDraw){

        Map<String, Object> model = new HashMap<String, Object>();
        List<Map<String,Object>> list = new ArrayList<>();
        UserSession userSession = getUserSession(request);

        List<Map<String, Object>> qrcodelist = qrcodeService.queryQrcodeInfoForDownload(id,isAutoDraw, userSession);
        for (Map<String, Object> map : qrcodelist) {

            String imgSrc= "";
            Map<String, Object> imgMap = new HashedMap();
            imgMap.put("id",map.get("id"));

            //其中 2：代表历史普通码瓶码 （营销码）；3：代表历史普通码箱码 （物流码） ；
            //4：组合码瓶码 物流码 ；5：代表组合码箱码（物流码）；6：代表普通码（营销码）7.普通码（物流码）
            Byte qrtype = NumberUtils.toByte(map.get("type").toString(), (byte) 1);
            if(isAutoDraw==1){
                imgSrc =  6+ "/" + map.get("uuid");

            }else if(isAutoDraw==0) {
                imgSrc =  7+ "/" + map.get("w_uuid");
            }else if(isAutoDraw==3) {
                imgSrc= (qrtype+1) + "/" + map.get("w_uuid");
            }else{
                imgSrc =  (qrtype+1) + "/" + map.get("uuid");
            }
            imgMap.put("imgSrc",imgSrc);
            list.add(imgMap);
        }


        model.put("list", list);

        return new ModelAndView("qr_image").addAllObjects(model);

    }

    //生成二维码图片
    @GetMapping("/getQrImage/{type}/{uuid}")
    @ResponseBody
    public void getBillsQrImage(HttpServletResponse response,@PathVariable Integer type, @PathVariable String uuid){

         type=type+1;
        String str = "https://scan.vats.com.cn/scan/"+ type + "/" + uuid;
        QRCodeUtil.createQrcode(str, response);
    }

    /**
     * 组合码
     * @param request
     * @param id
     * @param qcname
     * @return
     */
    @GetMapping("/qrcodegroup/qrcodeImage")
    public ModelAndView qrcodegroup(HttpServletRequest request, Integer id, String qcname){
        Map<String, Object> model = new HashMap<String, Object>();
        List<List> list = new ArrayList<>();
        UserSession userSession = getUserSession(request);

        List<Map<String, Object>> qrcodelist = qrcodeService.queryQrcodeGroupInfoForDownload(id, userSession);

        String tmp_puuid = "";
        List<Map<String, Object>> tlist = null;

        Map<String, Object> mymap = new HashMap<String, Object>();
        for (Map<String, Object> map : qrcodelist) {
            Long tid = NumberUtils.toLong(map.get("id").toString(), 0L);
            byte type = NumberUtils.toByte(map.get("type").toString(), (byte) 0);
            if (0 == type) {
                String uuid = map.get("uuid").toString();
                String p_uuid = map.get("p_uuid").toString();
                String w_uuid = map.get("w_uuid").toString();
                if (p_uuid.equals(tmp_puuid)) {
                    tlist = (List<Map<String, Object>>) mymap.get(p_uuid);
                } else {
                    tlist = new ArrayList<Map<String, Object>>();
                }

                Map<String, Object> tmap1 = new HashMap<String, Object>();
                tmap1.put("tid", tid);
                tmap1.put("uuid", uuid);
                tmap1.put("w_uuid", w_uuid);
                tlist.add(tmap1);
                mymap.put(p_uuid, tlist);
                tmp_puuid = p_uuid;
            }
        }


        for (Map<String, Object> map : qrcodelist) {
            byte type = NumberUtils.toByte(map.get("type").toString(), (byte) -1);
            if (1 == type) {
                List<Map<String,Object>> plist = new ArrayList<>();
                Map<String, Object> pr = new HashedMap();
                pr.put("id",map.get("id"));
                String uuidstr = map.get("uuid").toString();
                // 5 代表箱码物流码
                pr.put("imgSrc",5 + "/" + map.get("w_uuid"));
                plist.add(pr);
                tlist = (List<Map<String, Object>>) mymap.get(uuidstr);
                for (Map<String, Object> tmap2 : tlist) {
                    Map<String, Object> sr = new HashedMap();
                    sr.put("id",tmap2.get("tid"));
                    sr.put("imgSrc",4 + "/" + tmap2.get("w_uuid"));
                    plist.add(sr);

                }


                list.add(plist);
            }

        }




        model.put("list", list);
        return new ModelAndView("qr_image_group").addAllObjects(model);
    }
}
